﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using DataAccess;
using Hazop;
using Model;

namespace LOPA
{
    public partial class HazopTeamPage3 : UserControl{

        public HazopTeamPage3()
        {
            InitializeComponent();   
            _bindHazopTeam.DataSource = Program.Workspace.LopaProject.LopaTeamList;
            dgvHazopTeam.DataSource = _bindHazopTeam;
            _bindTitle.DataSource = Program.Workspace.TitleList;
            AddColumns();
        }

        private void AddColumns(){
            for (int i = 0; i < 50; i++){
                DataGridViewColumn calendarColumn = new DataGridViewTextBoxColumn();
                calendarColumn.HeaderText = "第"+(i+1)+"天";
                calendarColumn.Name = i.ToString();
              //  dgvHazopTeam.Columns.Add(calendarColumn);
                dgvHazopTeam.Columns.Insert(dgvHazopTeam.Columns.Count,calendarColumn);
            }
            for (int i = 0; i < Program.Workspace.LopaProject.LopaTeamList.Count; i++)
            {
                for (int j = 0; j < 50; j++){
                    if (Program.Workspace.LopaProject.LopaTeamList[i].DateList[j].Equals(""))
                         continue;
                    dgvHazopTeam.Rows[i].Cells[j.ToString()].Value = Program.Workspace.LopaProject.LopaTeamList[i].DateList[j];
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e){
            dgvHazopTeam.Enabled = true;
            for (int i = 0; i < Program.Workspace.LopaProject.LopaTeamList.Count; i++)
            {
                for (int j = 0; j < 50; j++)
                {
                    if (Program.Workspace.LopaProject.LopaTeamList[i].DateList[j].Equals(""))
                        continue;
                    dgvHazopTeam.Rows[i].Cells[j.ToString()].Value = Program.Workspace.LopaProject.LopaTeamList[i].DateList[j];
                }
            }
        }

        private void dgvHazopTeam_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvHazopTeam.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvHazopTeam.RowHeadersDefaultCellStyle.Font, rectangle,
                dgvHazopTeam.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        public void btnSave_Click(object sender, EventArgs e){
            for (int i = 0; i < dgvHazopTeam.Rows.Count; i++ )
            {   
                for (int j = 0; j < 50; j++){
                    if (dgvHazopTeam.Rows[i].Cells[j.ToString()].Value == null)
                    {
                      // continue;
                        Program.Workspace.LopaProject.LopaTeamList[i].DateList[j] = "";
                        continue;
                    }
                    string tmp = dgvHazopTeam.Rows[i].Cells[j.ToString()].Value.ToString();
                    Program.Workspace.LopaProject.LopaTeamList[i].DateList[j] = tmp;
//                    if (Program.Workspace.project.HazopTeam[i].DateList[j].Equals(""))
//                    {
//                        Program.Workspace.project.HazopTeam[i].DateList[j] = tmp;
//                    }
                }
            }
            Program.Workspace.SaveProject(MainForm.mainForm.SaveFilePath);
            dgvHazopTeam.Enabled = false;
            MessageBox.Show("LOPA小组保存成功!");
        }

        public void SaveWithoutRemider(){
            for (int i = 0; i < dgvHazopTeam.Rows.Count; i++)
            {
                for (int j = 0; j < 50; j++)
                {
                    if (dgvHazopTeam.Rows[i].Cells[j.ToString()].Value == null)
                    {
                        continue;
                    }
                    string tmp = dgvHazopTeam.Rows[i].Cells[j.ToString()].Value.ToString();
                    Program.Workspace.LopaProject.LopaTeamList[i].DateList[j] = tmp;
//                    if (Program.Workspace.project.HazopTeam[i].DateList[j].Equals(""))
//                    {
//                        Program.Workspace.project.HazopTeam[i].DateList[j] = tmp;
//                    }
                }
            }
            dgvHazopTeam.Enabled = false;
        }

        public DataGridView DgvHazopTeam{
            get { return dgvHazopTeam; }
            set { dgvHazopTeam = value; }
        }

        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(!btnEdit.Enabled){
                return;
            }
            HazopTeam item = new HazopTeam();
            for (int i = 0; i < 50; i++){
                item.DateList.Add("");
            }
            _bindHazopTeam.Insert(dgvHazopTeam.Rows.Count,item);
            dgvHazopTeam.Rows[dgvHazopTeam.Rows.Count - 1].Selected = true;
            dgvHazopTeam.CurrentCell = dgvHazopTeam[1, dgvHazopTeam.Rows.Count - 1];
        }





    }
}
